<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="匹配模式" prop="matchPattern">
      <el-radio-group v-model="dataForm.matchPattern">
        <el-radio :label="1">完全匹配</el-radio>
        <el-radio :label="2">包含匹配</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="关键词" prop="keywords">
      <el-input v-model="dataForm.keywords" placeholder="关键词"></el-input>
    </el-form-item>
    <el-form-item label="回复类型" prop="replyType">
      <el-radio-group v-model="dataForm.replyType">
        <el-radio :label="1">文字</el-radio>
        <el-radio :label="2">素材</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="文字内容" prop="content" v-if="dataForm.replyType == 1">
      <el-input v-model="dataForm.content" placeholder="文字内容"></el-input>
    </el-form-item>
    <el-form-item label="素材" v-if="dataForm.replyType == 2">
      <el-input v-model="dataForm.materialName">
        <el-button slot="append" @click="chooseMaterial">选择</el-button>
      </el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getWxKeywordsObj, addWxKeywordsObj, putWxKeywordsObj} from '@/api/mp/wxkeywords'

    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          matchPattern: 1,
          keywords: '',
          replyType: 1,
          content: '',
          materialId: '',
          materialName: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: '',
          tenantId: '',
        },
        dataRule: {
          matchPattern: [
            { required: true, message: '匹配模式(1-完全匹配,2-包含匹配)不能为空', trigger: 'blur' }
          ],
          keywords: [
            { required: true, message: '关键词不能为空', trigger: 'blur' }
          ],
          replyType: [
            { required: true, message: '回复类型(1-文字,2-素材)不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '文字内容不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '删除标识(1-未删除,-1已删除)不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户Id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxKeywordsObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(this.dataForm.replyType == 2){
                this.dataForm.content = this.dataForm.materialName
              }
            if (this.dataForm.id) {
                putWxKeywordsObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addWxKeywordsObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      },
      //选择素材
      chooseMaterial(){

      }
    }
  }
</script>
